package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Sfx;
	
	/**
	 * ...
	 * @author Edward Russell II
	 */
	public class Enemy extends Entity 
	{
		//Declare the variables we're going to use
		protected var image:Image;
		protected var enemyDeath:Sfx = new Sfx(GC.SFX_ENEMY_DEATH);
		protected var nodeList:XMLList;
		protected var currentPoint:int = 0;
		protected var _speed:Number = 0;
		
		
		public function Enemy(_name:String, _x:int,_y:int, speed:int = 0) 
		{
			//Setup the properties
			image = new Image(GC.GFX_ENEMY);
			graphic = image;
			setHitbox(image.scaledWidth, image.scaledHeight);
			type = "enemy";
			layer = -3;
			x = _x;
			y = _y;
			name = _name;
			_speed = speed;
			
			//populate the list of nodes for this enemy
			nodeList = GV.CURRENT_MAP.Entities.enemies.(@id == int(_name)).node;
			
		}
			
		override public function update():void 
		{
			
			//If the enemy goes off the left side of the screen, remove it
			if (x < FP.camera.x - width) world.remove(this);
			
			//If the enemy hits a bullet
			if (collide("bullet", x, y)) 
			{
				//Add the enemy's score value to the score
				GV.SCORE += GC.ENEMY_SCORE_VALUE;
				
				//set the high score
				if (GV.SCORE>=GV.HIGH_SCORE) GV.HIGH_SCORE = GV.SCORE;
				
				//Call the die() function
				die();
			}
			
			//if the enemy has a path, call the move() function
			if (nodeList.length() > 0) move();
			
			super.update();
		}
		
		public function die():void
		{
			//Play the death animation
			enemyDeath.play();
			
			//Remove the enemy
			world.remove(this)
		}
		
		override public function added():void 
		{
			GV.ENEMY_LIST.push(this);
			super.added();
		}
		
		public function move():void
		{
			//if the enemies X and Y value both are equal to the X and Y value of the node we're heading for then move on to the next node
			if (this.x == nodeList[currentPoint].@x && this.y == nodeList[currentPoint].@y && currentPoint < nodeList.length() - 1) currentPoint++;
			//if they aren't equal, then move towards our current node
			else if (this.x != nodeList[currentPoint].@x || this.y != nodeList[currentPoint].@y) FP.stepTowards(this, nodeList[currentPoint].@x, nodeList[currentPoint].@y, _speed);
			
		}
		
		
		
	}
}